package dao.reader.impl;

import dao.BaseDao;
import dao.reader.RegisterDao;
import entity.Borrow_card;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * RegisterDaoImpl
 *
 * @author lenovo
 * @since 2023/9/7
 */
public class RegisterDaoImpl extends BaseDao implements RegisterDao {
    @Override
    public int add(String username, String password) {
        String sql = "insert into borrow_card values(default,?,?,default,default)";
        return executeUpdate(sql,password,username);
    }

    @Override
    public Borrow_card getId(String password,String reader) {
        Borrow_card book=null;
        String sql = "select id from borrow_card where password=? and reader=? limit 1";
        Connection conn = getConnection();
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps=conn.prepareStatement(sql);
            ps.setString(1, password);
            ps.setString(2, reader);
            rs = ps.executeQuery();
            while (rs.next()) {
                book=new Borrow_card();
                book.setId(rs.getInt("id"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(rs,ps,conn);
        }
        return book;
    }
}
